package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.entity.DmMyReturnAmount;
import cn.com.cifi.mars.entity.MdmStage;
import cn.com.cifi.mars.mapper.mars.DmMyReturnAmountMapper;
import cn.com.cifi.mars.service.DmMyReturnAmountService;
import cn.com.cifi.mars.service.StageService;
import cn.com.cifi.mars.util.JDBCUtils;
import cn.com.cifi.mars.util.ListUtils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author JingYuanXiang
 * @since 2021-01-28
 */
@Slf4j
@Service
public class DmMyReturnAmountServiceImpl extends ServiceImpl<DmMyReturnAmountMapper, DmMyReturnAmount> implements DmMyReturnAmountService {

    @Autowired
    private DmMyReturnAmountMapper dmMyReturnAmountMapper;
    @Autowired
    private StageService stageService;

    int SALE_ROOM_UPDATE_SIZE = 1000;


    /**
     * 全量同步明源回款数据
     */
    @Override
    public void syncMyReturnData() {
        List<DmMyReturnAmount> dmMyReturnAmountList = new ArrayList<>();

        List<MdmStage> mdmStageList = stageService.selectAll();
        if(CollectionUtils.isEmpty(mdmStageList)){
            return;
        }
        Map<String, MdmStage> mdmStageMap = mdmStageList.stream().collect(Collectors.toMap(item -> item.getKingdeeProjectfId(), item -> item));

        try {
            //获取数据库连接
            Connection conn = new JDBCUtils().getConnection();
            CallableStatement cs = conn.prepareCall("{call Usp_C1_GetHkData_Bld()}");
            //是否查询成功
            boolean flag = cs.execute();
            if(flag){
                ResultSet rs = cs.getResultSet();
                while (rs.next()) {
                    try {
                        DmMyReturnAmount dmMyReturnAmount = new DmMyReturnAmount();
                        dmMyReturnAmount.setKingDeeBuGuid(rs.getString("金蝶公司ID"));
                        dmMyReturnAmount.setKingDeeBuName(rs.getString("金蝶公司名称"));
                        dmMyReturnAmount.setBuName(rs.getString("明源公司名称"));
                        dmMyReturnAmount.setKingDeeProGuid(rs.getString("主数据项目ID"));
                        dmMyReturnAmount.setKingDeeProName(rs.getString("主数据项目名称"));
                        dmMyReturnAmount.setProjName(rs.getString("明源项目名称"));
                        dmMyReturnAmount.setKingDeeProJfGuid(rs.getString("主数据分期ID"));
                        dmMyReturnAmount.setKingDeeProJfName(rs.getString("主数据分期名称"));
                        dmMyReturnAmount.setProjectId(mdmStageMap.get(dmMyReturnAmount.getKingDeeProJfGuid()).getProjectId());
                        dmMyReturnAmount.setStageId(mdmStageMap.get(dmMyReturnAmount.getKingDeeProJfGuid()).getProjectFid());
                        dmMyReturnAmount.setProductName(rs.getString("业态名称"));
                        dmMyReturnAmount.setKingDeeBldGuid(rs.getString("主数据楼栋ID"));
                        dmMyReturnAmount.setFactMonth(rs.getString("月份"));
                        dmMyReturnAmount.setHkAmount(rs.getBigDecimal("回款金额"));
                        dmMyReturnAmount.setZlAmount(rs.getBigDecimal("增量"));
                        dmMyReturnAmount.setClAmount(dmMyReturnAmount.getHkAmount().subtract(dmMyReturnAmount.getZlAmount()));
                        dmMyReturnAmountList.add(dmMyReturnAmount);
                    }catch (Exception e){
                        log.error(e.getMessage());
                        System.out.println(rs.getString(1)+" : "+rs.getString(2)
                                + rs.getString(3) + " : " + rs.getString(4) + " : "
                                + rs.getString(5) + " : "+ rs.getString(6) + " : "
                                + rs.getString(7) + " : " + rs.getString(8) + " : "
                                + rs.getString(9) + " : " + rs.getString(10) + " : "
                                + rs.getString(11) + " : " + rs.getString(12) + " : "
                                + rs.getString(13));
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        //批量插入一次1000条
        if(org.apache.commons.collections.CollectionUtils.isNotEmpty(dmMyReturnAmountList)){
            List<List> subList = ListUtils.getSubList(dmMyReturnAmountList, SALE_ROOM_UPDATE_SIZE);
            for (List list : subList) {
                List<DmMyReturnAmount> insertList = (List<DmMyReturnAmount>) list;
                dmMyReturnAmountMapper.insertList(insertList);
            }
        }
    }

    /**
     * 单分期同步明源回款数据
     * @param stageId
     */
    @Override
    public void syncMyReturnDataByStageId(String stageId) {
        MdmStage mdmStage = stageService.selectById(stageId);
        if(null == mdmStage){
            return;
        }

        try {
            //获取数据库连接
            Connection conn = new JDBCUtils().getConnection();
            CallableStatement cs=conn.prepareCall("{call Usp_GXC_GetHkData_Detail(?)}");
            cs.setString(1,"454FFEF9-AF7C-E711-80B9-005056A21B76");

            //是否查询成功
            boolean flag = cs.execute();
            if(flag){
                ResultSet rs = cs.getResultSet();
                while (rs.next()) {
                    DmMyReturnAmount dmMyReturnAmount = new DmMyReturnAmount();

                    dmMyReturnAmount.setKingDeeBuGuid(rs.getString("KingDeeBUGUID"));
                    dmMyReturnAmount.setKingDeeBuName(rs.getString("KingDeeBuName"));
                    dmMyReturnAmount.setBuName(rs.getString("BuName"));
                    dmMyReturnAmount.setKingDeeProGuid(rs.getString("KingDeeProjGUID"));
                    dmMyReturnAmount.setKingDeeProName(rs.getString("KingDeeProjName"));
                    dmMyReturnAmount.setProjectId(mdmStage.getProjectId());
                    dmMyReturnAmount.setStageId(mdmStage.getProjectFid());
                    dmMyReturnAmount.setProjName(rs.getString("ProjName"));
                    dmMyReturnAmount.setProductName(rs.getString("ProductName"));
                    dmMyReturnAmount.setFactMonth(rs.getString("FactMonth"));
                    dmMyReturnAmount.setHkAmount(rs.getBigDecimal("HkAmount"));
                    dmMyReturnAmount.setZlAmount(rs.getBigDecimal("ZlAmount"));
                    dmMyReturnAmount.setClAmount(dmMyReturnAmount.getHkAmount().subtract(dmMyReturnAmount.getZlAmount()));
                    log.info(dmMyReturnAmount.toString());
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            System.out.println("查询失败");
        }


    }

}
